package com.ai.sml.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.util.JsonUtil;
import com.ai.sml.control.IControlService;
/**
 * redis存入数据
 * @author limeng
 *
 */
public class CacheUtil {

	static ApplicationContext context = new ClassPathXmlApplicationContext(
			"spring/spring-services.xml");
	/**
	 * 根据key往redis放入值
	 * @param cacheKey
	 * @param value
	 */
	public static void put2Cache(String cacheKey, String value){
		IControlService controlService = (IControlService)context.getBean("controlService");
		InputObject inputObject =new InputObject();
		inputObject.setService("mallCommonService");
		inputObject.setMethod("put2Cache");
		inputObject.getParams().put("cacheKey", cacheKey);
		inputObject.getParams().put("value", value);
		controlService.execute(inputObject);
		
	}
	/**
	 * redis中放入指定时间的值
	 * @param cacheKey
	 * @param value
	 * @param seconds
	 */
	public static void put2Cache(String cacheKey, String value, int seconds){
		IControlService controlService = (IControlService)context.getBean("controlService");
		InputObject inputObject =new InputObject();
		inputObject.setService("mallCommonService");
		inputObject.setMethod("put2CacheTime");
		inputObject.getParams().put("cacheKey", cacheKey);
		inputObject.getParams().put("value", value);
		inputObject.getParams().put("time", Integer.toString(seconds));
		controlService.execute(inputObject);
		
	}
	/**
	 * 从redis中获取数据
	 * @param cacheKey
	 * @return
	 */
	public static String getFromCache(String cacheKey){
//		if(null!=cacheKey)
//		{
			IControlService controlService = (IControlService)context.getBean("controlService");
			InputObject inputObject =new InputObject();
			inputObject.setService("mallCommonService");
			inputObject.setMethod("getFromCache");
			inputObject.getParams().put("cacheKey", cacheKey);
			OutputObject outputObject= controlService.execute(inputObject);
			return outputObject.getBean().get("value");
//		}
//		return "";
		
	}
	/**
	 * 从redis中删除数据
	 * @param cacheKey
	 */
	public static void delFromCache(String cacheKey){
//		if(null!=cacheKey)
//		{
			IControlService controlService = (IControlService)context.getBean("controlService");
			InputObject inputObject =new InputObject();
			inputObject.setService("mallCommonService");
			inputObject.setMethod("delFromCache");
			inputObject.getParams().put("cacheKey", cacheKey);
			controlService.execute(inputObject);
//		}
		
	}

	public static Integer rpush(String cacheKey, String[] value, int seconds) {
		IControlService controlService = (IControlService) context.getBean("controlService");
		InputObject inputObject = new InputObject();
		inputObject.setService("mallCommonService");
		inputObject.setMethod("rpush");
		inputObject.getParams().put("cacheKey", cacheKey);
		inputObject.getParams().put("value", JsonUtil.convertObject2Json(value));
		inputObject.getParams().put("time", Integer.toString(seconds));
		OutputObject outputObject = controlService.execute(inputObject);
		return Integer.valueOf(outputObject.getBean().get("length"));
	}
	
	public static Long incr(String cacheKey) {
		IControlService controlService = (IControlService) context.getBean("controlService");
		InputObject inputObject = new InputObject();
		inputObject.setService("mallCommonService");
		inputObject.setMethod("incr");
		inputObject.getParams().put("cacheKey", cacheKey);
		OutputObject outputObject = controlService.execute(inputObject);
		return Long.valueOf(outputObject.getBean().get("value"));
	}
	
	public static boolean setnx(String cacheKey, String value) {
		IControlService controlService = (IControlService) context.getBean("controlService");
		InputObject inputObject = new InputObject();
		inputObject.setService("mallCommonService");
		inputObject.setMethod("setnx");
		inputObject.getParams().put("cacheKey", cacheKey);
		inputObject.getParams().put("value", value);
		OutputObject outputObject = controlService.execute(inputObject);
		return Boolean.valueOf(outputObject.getBean().get("result"));
	}
	
	public static void main(String[] args) {
		CacheUtil.put2Cache("testlm", "aaa");
		System.out.println(CacheUtil.getFromCache("testlm"));
		CacheUtil.delFromCache("testlm");
		System.out.println(CacheUtil.getFromCache("testlm"));
	}

}
